/**
 *  auth: Wayne
 *  function: 对外用户接口
 * */

let express = require('express');
let router = express.Router();
let user = require("../model/user");
let util=require("../util/util");
let vps = require("../model/vps");


/**
 * @function 查询有效用户,并获取proxy(第二次获取)
 * @author wayne
 * @date 2017-11-20
 * @param
 */
router.get('/getvaliduser',function(req,res){
    const newObj = new user(req.body);
    let obj={},
        param={};
    obj.ip = '0.0.0.1'
    obj.port='0'//'6667'
    obj.secret='27c8815bbc5ccc3fa9d1964de0cdecaf'
    if(req.query.phone&&req.query.phone!=='(null)')
    {
        param.phone=req.query.phone
        user.find(param, (err,results)=> {
            if(err){
                res.send(obj);
            }else {
                if(results.length>0){
                    if(results[0].status!=="1")//状态为1，发送无效的地址
                    {
                        res.send(obj);
                    }
                    else
                    {//查询有效的vps
                        vps.findOne({"line" : "IOS","status" : "1","$where": "this.maxuser > this.currentuser"}).then(record => {
                            obj.port=record.port||"443"
                            obj.host=record.host
                            obj.secret=record.secret
                            obj.ip=record.host
                            if(!results[0].vps.host)
                            {//如果当前用户的vps为空，更新其vps
                                user.findOneAndUpdate(param, { $set: { vps: obj }},(err,updateresult)=>{
                                    res.send(obj);
                                }).catch(err=>{})
                            }
                            else
                            {
                                res.send(obj);
                            }

                        }).catch(err => {
                            res.send(obj);
                        });

                    }
                }else{//如果用户不存在，新增该用户，并传输有效地址
                    newObj.status="1";
                    newObj.phone=param.phone;
                    newObj.uname=param.phone;

                    vps.findOne({"line" : "IOS","status" : "1","$where": "this.maxuser > this.currentuser"}).then(record => {
                        if(record.host)
                        {
                            obj.port=record.port||"443"
                            obj.host=record.host
                            obj.ip=record.host
                            obj.secret=record.secret
                            let currentuser=parseInt(record.currentuser)+1
                            newObj.vps=obj
                            newObj.save( (err, result)=>{
                                vps.findOneAndUpdate({host:obj.host}, { $set: { currentuser: currentuser }},(err,updateresult)=>{

                                }).catch(err=>{})
                            })
                            res.send(obj);
                        }
                    else
                        {
                            res.send(obj);
                        }



                    }).catch(err => {
                        res.send(obj);
                    });

                }
            }
        })

    }
    else
    {//该地址主要用来获取验证码
        obj.port='443'//'6667'
        obj.ip='149.28.65.112'//'207.148.72.56'
        obj.secret='27c8815bbc5ccc3fa9d1964de0cdecaf'//'3e8275a5a142fa8c20bc19292b12368b'
        res.send(obj);
    }

})


module.exports = router;
